package cn.xy

import java.util.concurrent.atomic.AtomicInteger

/**
  * Created by xiaoyuan on 2017/11/24.
  */
object Atom {
  def main(args: Array[String]): Unit = {
    //原子类，加减都是原子的操作
    var index : AtomicInteger = new AtomicInteger(0);

    var index2:Int =0;

    var index3:Int =0;

    (0 to 1000000).par.foreach {
      x=>{
       index.getAndIncrement();
      }
    }

    (0 to 1000000).par.foreach {
      x=>{
        synchronized{
          index2+=1
        }
        //println(Thread.currentThread.getName)
      }
    }

    (0 to 1000000).par.foreach {
      x=>{
          index3+=1
      }
    }

    println(index.get());
    println(index2)
    println(index3)

  }
}
